package de.fhkl.android.activities;

import java.util.ArrayList;

import de.fhkl.android.R;
import de.fhkl.android.utils.ExpandableListViewFactory;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class SettingActivity extends Activity {
	private String tag = "SettingActivity";

	public void onCreate(Bundle savedInstanceState) {
		Log.i(tag, "begin");
		super.onCreate(savedInstanceState);
		ExpandableListView view;
		view = createView();
		view.setGroupIndicator(null);
		setContentView(view);
		Log.i(tag, "end");
	}

	private ExpandableListView createView() {
		ExpandableListViewFactory factory = new ExpandableListViewFactory(this);
		final Context context = this;
		/**
		 * Menu1 - Login Information
		 */
		View menu1 = createMenu(android.R.drawable.ic_menu_myplaces,
				R.string.login_info);
		menu1.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				AlertDialog.Builder builder = new AlertDialog.Builder(context);
				builder.setTitle(R.string.login_info);

				String username = getUsername();
				String password = getPassword();

				LinearLayout menu1DialogView = new LinearLayout(context);
				menu1DialogView.setOrientation(1);
				menu1DialogView.setLayoutParams(new LayoutParams(
						LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

				/**
				 * Username and Password EditText
				 */
				final EditText usernameInput = new EditText(context);
				final EditText passwordInput = new EditText(context);
				final CheckBox saveInfo = new CheckBox(context);
				final TextWatcher watcher;
				usernameInput.setText(username);
				passwordInput.setText(password);
				usernameInput.setHint(R.string.username);
				passwordInput.setHint(R.string.password);
				passwordInput
						.setTransformationMethod(PasswordTransformationMethod
								.getInstance());
				/**
				 * TextWatcher. Only when the both EditTexts are not empty,check
				 * the saveInfo.
				 */
				watcher = new TextWatcher() {
					public void afterTextChanged(Editable arg0) {
					}

					public void beforeTextChanged(CharSequence s, int start,
							int count, int after) {
					}

					public void onTextChanged(CharSequence s, int start,
							int before, int count) {
						if (usernameInput.getText().length() == 0
								|| passwordInput.getText().length() == 0) {
							saveInfo.setChecked(false);
						} else {
							saveInfo.setChecked(true);
						}
					}
				};
				// add TextWatcher.
				usernameInput.addTextChangedListener(watcher);
				passwordInput.addTextChangedListener(watcher);

				// Save Login Information as checkbox
				saveInfo.setText(R.string.saveInfo);
				if (username.equals("") || password.equals("")) {
					saveInfo.setChecked(false);
				} else {
					saveInfo.setChecked(true);
				}
				saveInfo.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {
						boolean isChecked = saveInfo.isChecked();
						if (isChecked) {
							if (usernameInput.getText().length() == 0
									|| passwordInput.getText().length() == 0) {
								saveInfo.setChecked(false);
							}
						} else {
							usernameInput.setText("");
							passwordInput.setText("");
							usernameInput.requestFocus();
						}
					}
				});

				menu1DialogView.addView(usernameInput);
				menu1DialogView.addView(passwordInput);
				menu1DialogView.addView(saveInfo);
				builder.setView(menu1DialogView);
				builder.setPositiveButton(android.R.string.ok,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								if (saveInfo.isChecked()) {

									updateUsername(usernameInput.getText()
											.toString());
									updatePassword(passwordInput.getText()
											.toString());
								} else {
									updateUsername("");
									updatePassword("");
								}
							}
						});
				builder.setNegativeButton(android.R.string.cancel,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								dialog.dismiss();
							}
						});
				AlertDialog dialog = builder.create();
				dialog.show();
			}

		});
		factory.addGroup(menu1);

		/**
		 * Menu2 - Location
		 */
		View menu2 = createMenu(android.R.drawable.ic_menu_mylocation,
				R.string.campus_location);
		menu2.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				AlertDialog.Builder builder = new AlertDialog.Builder(context);
				builder.setTitle(R.string.location);
				final ArrayList<Integer> indexList = new ArrayList<Integer>();
				int index = getLocationIndex();
				builder.setSingleChoiceItems(R.array.location_array, index,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int which) {
								indexList.add(new Integer(which));
							}
						});
				builder.setPositiveButton(android.R.string.ok,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								int size = indexList.size();
								if (size > 0) {
									int index = indexList.get(size - 1);
									updateLocation(index);
								}
							}
						});
				builder.setNegativeButton(android.R.string.cancel,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								dialog.dismiss();
							}
						});

				AlertDialog dialog = builder.create();
				dialog.show();
			}

		});
		factory.addGroup(menu2);

		/**
		 * Menu3 - Language
		 */
		View menu3 = createMenu(android.R.drawable.ic_menu_mapmode,
				R.string.language);
		menu3.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				AlertDialog.Builder builder = new AlertDialog.Builder(context);
				builder.setTitle(R.string.language);
				final ArrayList<Integer> indexList = new ArrayList<Integer>();
				int index = getLanguageIndex();
				builder.setSingleChoiceItems(R.array.language_array, index,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int which) {
								indexList.add(new Integer(which));
							}
						});
				builder.setPositiveButton(android.R.string.ok,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								int size = indexList.size();
								if (size > 0) {
									int index = indexList.get(size - 1);
									updateLanguage(index);
								}
							}
						});
				builder.setNegativeButton(android.R.string.cancel,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								dialog.dismiss();
							}
						});

				Dialog dialog = builder.create();
				dialog.show();
			}

		});
		factory.addGroup(menu3);

		return factory.getRoot();
	}

	private View createMenu(int iconResId, int titleResId) {
		LinearLayout menu = new LinearLayout(this);
		ImageView icon = new ImageView(this);
		icon.setImageResource(iconResId);
		icon.setPadding(20, 5, 20, 5);
		TextView title = new TextView(this);
		title.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.FILL_PARENT));
		title.setGravity(Gravity.CENTER_VERTICAL);
		title.setText(titleResId);
		title.setTextSize(20);
		menu.addView(icon);
		menu.addView(title);
		return menu;
	}

	private String getUsername() {
		return "";
	}

	private String getPassword() {
		return "";
	}

	private int getLocationIndex() {
		return 2;
	}

	private int getLanguageIndex() {
		return 0;
	}

	private void updateUsername(String username) {
		Log.i(tag, "username=" + username);
	}

	private void updatePassword(String password) {
		Log.i(tag, "password=" + password);
	}

	private void updateLocation(int index) {
		Log.i(tag, "updateLocation=" + index);
	}

	private void updateLanguage(int index) {
		Log.i(tag, "updateLanguage=" + index);
	}
}
